clear
set more      off
set varabbrev off
set gr        off
set linesize 255

/* INPUTS */

*cd "/data2/cedsei/data2"

********************************************************************************
/*
cap log close 
log using ols.log, replace

local co = 1
local replace replace 

local coefs = 1
if `coefs'==1 {
	foreach vv in newjob_hu2 {
	foreach kk in search_ext search_ind2 search_ihs {
	foreach num of numlist 1(1)2 {
	clear 
	use "/data2/cedsei/data2/A1_aku_prep"
	
	if `num'==2 {
		gen temp = emp==0 
		egen une_ever = max(temp), by(lopnr)
		egen emp_ever = max(emp) , by(lopnr)
		keep if une_ever == 1 & emp_ever == 1
		drop une_ever emp_ever temp
	}

	gen age = year - fodelsear  - 40
	gen age2 = age*age 
	gen age3 = age*age*age
	if "`kk'"=="search_ihs" {
		g     `kk' = ln(search_int       + sqrt(search_int^2 +1))
	}
	gen emp_se = cond(emp==1,`kk',0)
	gen searche = `kk' // cond(emp==0,`kk',0)
	gen temp = 1
	egen x = group(emp lopnr)
	
	sum searche if emp==1 
	local indep_emp = round(10000*r(mean))/10000
	sum searche if emp==0 
	local indep_une = round(10000*r(mean))/10000
	
	tempfile tt 
	save 	`tt', replace

	reg `vv' emp, cluster(lopnr)

	regsave 
	sum coef if var=="emp"
	local empcoef = r(mean)
	sum stderr if var=="emp"
	local empstde = r(mean)
	sum coef if var=="_cons"
	local unempcoef = r(mean)
	sum stderr if var=="_cons"
	local unempstde = r(mean)
	
	clear 
	use `tt' 
	
	if `num'==1 {
		reghdfe `vv' searche emp_se  i.month##i.emp i.year##i.emp i.emp##i.woman##i.edu##c.age i.emp##i.woman##i.edu##c.age2 i.emp##i.woman##i.edu##c.age3, absorb(temp) cluster(lopnr)
	}
	else if `num'==2 {
		reghdfe `vv' searche emp_se emp  i.year##i.emp i.emp##i.woman##i.edu##c.age i.emp##i.woman##i.edu##c.age2 i.emp##i.woman##i.edu##c.age3, absorb(x) cluster(lopnr)
	}
	local u_co = _b[searche]
	local u_se = _se[searche]
	local e_co = _b[emp_se]
	local e_se = _se[emp_se]
	local co_chi = (`u_co'+`e_co')/`u_co'
	di `u_co'
	di `e_co'
	di `u_se'
	di `e_se'
	local se_chi = sqrt( (1/`u_co'^2)*((`e_se'^2) + ((`e_co'/`u_co')^2)*`u_se'^2 ))
	di "`co_chi'; `se_chi'"
	di "`replace'"
	if `num'==1 {
		regsave searche emp_se _cons 1.emp using "$datapath/coefs_ols_20230524", pval autoid `replace' addlabel(rhs,"`kk'", spec,"`num'", ratio_est,"`co_chi'", ratio_se,"`se_chi'",indep_emp,"`indep_emp'",indep_une,"`indep_une'") table(col_`co', asterisk(10 5 1) parentheses(stderr) format(%7.3f))
	}
	else if `num'==2 {
		regsave searche emp_se             using "$datapath/coefs_ols_20230524", pval autoid `replace' addlabel(rhs,"`kk'", spec,"`num'", ratio_est,"`co_chi'", ratio_se,"`se_chi'",indep_emp,"`indep_emp'",indep_une,"`indep_une'") table(col_`co', asterisk(10 5 1) parentheses(stderr) format(%7.3f))
	}	
	local replace append
	local co = `co' + 1

	}
	}
	}
}

log close 
*/
clear 
use "$datapath/coefs_ols_20230524"

drop if inlist(var,"searche_pval","emp_se_pval","_id", "spec","1.emp_pval","_cons_pval")

gen temp = 0 if var =="rhs"
replace temp = 1 if var=="searche_coef"
replace temp = 2 if var=="searche_stderr"
replace temp = 3 if var=="emp_se_coef"
replace temp = 4 if var=="emp_se_stderr"

replace temp = 5 if var=="1.emp_coef"
replace temp = 6 if var=="1.emp_stderr"
replace temp = 7 if var=="_cons_coef"
replace temp = 8 if var=="_cons_stderr"

replace temp = 9 if var=="ratio_est"
replace temp = 10 if var=="ratio_se"

replace temp = 11 if var=="r2"
replace temp = 12 if var=="N"
sort temp 
drop temp


foreach num of numlist 1(1)6 {
	gen temp =  col_`num' if var=="ratio_se" 
	destring temp, replace 
	gen temp2 =  col_`num' if var=="ratio_est" 
	destring temp2, replace 
	
	sum temp2, d
	local n = r(mean) -1 
	sum temp, d 
	local t = `n'/r(mean)
	di `t'
	
	tostring temp, replace force
	tostring temp2, replace force
	replace col_`num' = "(0" + temp+ ")" if var=="ratio_se"
	if `t'>2.576 {
		replace col_`num' =  col_`num' + "***"     if var=="ratio_est"
	}
	else if `t'>1.960 {
		replace col_`num' =  col_`num' + "**"     if var=="ratio_est"		
	}
	drop temp* 
}
	
local nrows = _N 
foreach num of numlist 1(1)6 {
	foreach row of numlist 2(1)`nrows' {
		local add_content = col_`num'[`row']
		cap file close f 
		file open f using "$agetablepath/Search/reg_table_col`num'_row`row'.tex", replace write 
		file write f "`add_content'"
		file close f 
	}
}
ss

log close 
